package tripx.user;

import java.util.HashMap;

import tripx.server.Service;
import tripx.sql.SqlHandler;
import tripx.sql.UpdateSqlHandler;

/**
 * The standard user interfaces, provide methods to operate on the User table
 * with the regular request.
 * 
 * @author zhb
 * 
 */
public class UserService extends Service {

	UserServiceConfig config;

	UpdateSqlHandler userHandler;

	public UserService() {
		config = UserServiceConfig.getInstance();
		userHandler = new UpdateSqlHandler(config.getUserTableName());

		sqlHandler = new SqlHandler(config.getQueryActionList());
	}

	/**
	 * add a new User to the database. This method will not guarantee this user
	 * does not exist in the database
	 * 
	 * @param user
	 * @return
	 */
	public boolean addUser(DbUser user) {
		String sql = userHandler.getSqlAdd(user);
		if (userHandler.getStatus() == 1 && invoker.executeUpdateSql(sql) == 1) {
			return true;
		}
		return false;
	}

	/**
	 * modify the user attribute.
	 * 
	 * @param key
	 * @param newValue
	 * @param conditionMap
	 * @return
	 */
	public boolean modifyUser(String key, String newValue,
			HashMap<String, String> conditionMap) {
		String sql = userHandler.getSqlModify(key, newValue, conditionMap);
		if (userHandler.getStatus() == 1 && invoker.executeUpdateSql(sql) > 0) {
			return true;
		}
		return false;
	}
}
